package com.jpcc.vw.onlineRadio.service;

import android.content.Context;
import android.media.AudioManager;

import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.utils.PreferenceUtils;
import com.orhanobut.logger.Logger;

import static android.content.Context.AUDIO_SERVICE;

/**
 * Created by kanshan on 2018
 */
public class AudioFocusManager implements AudioManager.OnAudioFocusChangeListener {
    private AudioManager audioManager;
    private boolean isPausedByFocusLossTransient;

    public AudioFocusManager(Context context) {
        audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
    }

    public boolean requestAudioFocus() {
        return audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)
                == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
    }

    public AudioManager getAudioFocusManager() {
        return audioManager;
    }

    public boolean requestAudioFocus1() {
        return audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int i) {

            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)
                == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
    }

    public void abandonAudioFocus() {
        audioManager.abandonAudioFocus(this);
    }

    @Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            // 重新获得焦点
            case AudioManager.AUDIOFOCUS_GAIN:
                Constants.FLAG_FOCUS_ON_SELF = true;
                if (isPausedByFocusLossTransient) {
                    // 通话结束，恢复播放
//                    AlbumRadioAudioPlayer.get().startPlayer();
                }
                Logger.i("onAudioFocusChange-->AUDIOFOCUS_GAIN");
                Logger.i("isPlaying()-->" + AlbumRadioAudioPlayer.get().isPlaying());
                Logger.i("FLAG_PAUSE_MANUAL-->" + Constants.FLAG_PAUSE_MANUAL);
//                if ((!AlbumRadioAudioPlayer.get().isPlaying()) && (!Constants.FLAG_PAUSE_MANUAL)) {
//                    AlbumRadioAudioPlayer.get().startPlayer();
//                }


                //以下是最新的焦点策略实现
                AlbumRadioAudioPlayer.get().startPlayer();
                PreferenceUtils.SaveAudioFocusStatus(true);
                break;
            // 永久丢失焦点，如被其他播放器抢占
            case AudioManager.AUDIOFOCUS_LOSS:
                Constants.FLAG_FOCUS_ON_SELF = false;
//                AlbumRadioAudioPlayer.get().playPauseTemporary();
//                Constants.FLAG_PAUSE_MANUAL=false;
                AlbumRadioAudioPlayer.get().pausePlayerTemporary();
                PreferenceUtils.SaveAudioFocusStatus(false);
                Logger.i("onAudioFocusChange-->AUDIOFOCUS_LOSS");
                Logger.i("isPlaying()-->" + AlbumRadioAudioPlayer.get().isPlaying());
                Logger.i("FLAG_PAUSE_MANUAL-->" + Constants.FLAG_PAUSE_MANUAL);
                break;
            // 短暂丢失焦点，如来电
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Constants.FLAG_FOCUS_ON_SELF = false;
//                AlbumRadioAudioPlayer.get().playPause();
                AlbumRadioAudioPlayer.get().pausePlayerTemporary();
                isPausedByFocusLossTransient = true;
                PreferenceUtils.SaveAudioFocusStatus(false);
//                Constants.FLAG_PAUSE_MANUAL=false;
                Logger.i("onAudioFocusChange-->AUDIOFOCUS_LOSS_TRANSIENT");
                Logger.i("isPlaying()-->" + AlbumRadioAudioPlayer.get().isPlaying());
                Logger.i("FLAG_PAUSE_MANUAL-->" + Constants.FLAG_PAUSE_MANUAL);
                break;
            // 瞬间丢失焦点，如通知
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                Constants.FLAG_FOCUS_ON_SELF = false;
                PreferenceUtils.SaveAudioFocusStatus(false);
//                Constants.FLAG_PAUSE_MANUAL=false;
                // 音量减小为一半
                AlbumRadioAudioPlayer.get().getMediaPlayer().setVolume(0.5f, 0.5f);
                Logger.i("onAudioFocusChange-->AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                Logger.i("isPlaying()-->" + AlbumRadioAudioPlayer.get().isPlaying());
                Logger.i("FLAG_PAUSE_MANUAL-->" + Constants.FLAG_PAUSE_MANUAL);
                break;
        }
    }
}
